<?php
#基础函数
include_once('_path.php');

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

#处理缩略图
include_once(_CONST_LOCAL_INCLUDE_PATH.'phpthumb/ThumbLib.inc.php');

#taobao
include_once(_CONST_LOCAL_INCLUDE_PATH."/taobao/taobao.class.php");

if ($_REQUEST['notify_result']=='success'){
	$t = new openTaoBao( TB_AKEY , TB_SKEY, TB_CALLBACK_URL);
	unset($_GET['sign']);
	$t->param = $_GET;
	$sign = $t->createSign();
	if($_REQUEST['app_key']==TB_AKEY && $_REQUEST['sign'] == $sign){
		    $uid = $_REQUEST['taobao_user_id'];
		
		if($uid>0){
			#判断是否已经注册
			$rs = $database->queryFirst("SELECT id, name FROM user WHERE wb_id=".string_sql($uid)." AND wb_type=".string_sql('taobao'));
			if(!empty($rs['id']))
			{
				$_SESSION['userid'] = $rs['id'];
				$_SESSION['name'] = $rs['name'];
			}
			else
			{
				#获得注册信息进行注册
				
				$name = string_sql($_REQUEST['taobao_user_nick']);
				$email = string_sql('');
				$lasttime = string_sql(_CONST_TIMENOW);
				$lastip = string_sql(fetch_alt_ip());
				$wb_id = string_sql($uid);
			    $wb_type = string_sql('taobao');
				
				$database->execute("INSERT INTO user (name, email, lasttime, lastip, wb_id, wb_type) VALUES ("
				.$name." ,".$email." ,"
				.$lasttime." ,".$lastip." ,".$wb_id." ,".$wb_type
				.");");
				
				if ($id = $database->getInsertId()) {
					$_SESSION['userid'] = $id;
					$_SESSION['name'] = $_REQUEST['taobao_user_nick'];
				}			
				#更新info信息
				$database->execute("INSERT INTO user_info (user_id) VALUES (".$id.");");
			}
		}
	}
}
header("Location:" . _CONST_WEB_URL . 'index.php');
exit;
?>